﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pibby.Core.Views;
using Pibby.Core;
using Pibby.Core.Opml;
using Pibby.Controls;

namespace Pibby.UI
{
	public partial class SubscriptionManagement : Form, ISubscriptionListView
	{
		public SubscriptionManagement()
		{
			InitializeComponent();
		}

		public event EventHandler<FilePersistenceEventArgs> ImportPodcasts;

		public event EventHandler<FilePersistenceEventArgs> ExportPodcasts;

		public event EventHandler<NewPodcastEventArgs> AddPodcast;

		public event EventHandler<RemovePodcastEventArgs> RemovePodcast;

		public event EventHandler<PodcastEventArgs> SelectPodcast;
		public event EventHandler<RenamePodcastEventArgs> RenamePodcast;
		public event EventHandler<PodcastEventArgs> ShowPodcastBackCatalog;

		public void ShowPodcasts(PodcastCollection podcasts)
		{
			this.subscriptionView1.ShowPodcasts(podcasts);
			this.subscriptionView1.removeButton.Enabled = false;
		}

		public void RefreshPodcasts(Core.PodcastCollection podcasts)
		{
			//throw new NotImplementedException();
		}

		public void ShowPodcastInfo(Core.Podcast podcast)
		{
			//throw new NotImplementedException();
		}

		public void ReportError(string message)
		{
			//throw new NotImplementedException();
		}

		public event EventHandler Initialising;

		public event EventHandler InitialiseComplete;

		public void Start()
		{
			throw new NotImplementedException();
		}

		public void Stop()
		{
			throw new NotImplementedException();
		}

		private void SubscriptionManagement_Load(object sender, EventArgs e)
		{
			if (this.Initialising != null)
				this.Initialising(this, EventArgs.Empty);

			this.subscriptionView1.InitialiseUI();

			this.subscriptionView1.addButton.Click += new EventHandler(addButton_Click);
			this.subscriptionView1.removeButton.Click += new EventHandler(removeButton_Click);
			this.subscriptionView1.importButton.Click += new EventHandler(importButton_Click);
			this.subscriptionView1.exportButton.Click += new EventHandler(exportButton_Click);
			this.subscriptionView1.feedListView.SelectedIndexChanged += new EventHandler(feedListView_SelectedIndexChanged);
			this.subscriptionView1.feedListView.AfterLabelEdit += new LabelEditEventHandler(feedListView_AfterLabelEdit);

			this.subscriptionView1.feedListView.SmallImageList = this.imageList1;

			// 
			if (this.InitialiseComplete != null)
				this.InitialiseComplete(this, EventArgs.Empty);
		}

		void feedListView_AfterLabelEdit(object sender, LabelEditEventArgs e)
		{
			// Determine if label is changed by checking for null.
			if (e.Label == null)
				return;

			var rename = RenamePodcast;

			if (rename != null)
			{
				var item = this.subscriptionView1.feedListView.Items[e.Item] as PodcastListViewItem;

				if (item != null)
				{
					rename (this, new RenamePodcastEventArgs(item.Id, e.Label));
				}
			}
		}

		void feedListView_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.subscriptionView1.removeButton.Enabled = (this.subscriptionView1.feedListView.SelectedItems.Count > 0);

		}

		void addButton_Click(object sender, EventArgs e)
		{
			using (var addDialog = new AddPodcastForm())
			{
				if (DialogResult.OK == addDialog.ShowDialog())
				{
					this.AddFeed(addDialog.SelectedFeed, addDialog.SelectedName);
				}
			}
		}

		private void AddFeed(string url, string name)
		{
			EventHandler<NewPodcastEventArgs> addEvent = AddPodcast;

			if (addEvent != null)
			{
				addEvent(this, new NewPodcastEventArgs(url, name));
			}
		}


		void removeButton_Click(object sender, EventArgs e)
		{
			if (this.subscriptionView1.feedListView.SelectedItems.Count > 0)
			{
				foreach (ListViewItem item in this.subscriptionView1.feedListView.SelectedItems)
				{
					var podcast = item as PodcastListViewItem;

					if (podcast != null)
					{
						using (RemovePodcastForm form = new RemovePodcastForm())
						{
							form.Podcast = podcast.Text;

							DialogResult result = form.ShowDialog();

							if (result == DialogResult.OK)
							{
								EventHandler<RemovePodcastEventArgs> removeEvent = RemovePodcast;

								if (removeEvent != null)
								{
									removeEvent(this, new RemovePodcastEventArgs(podcast.Id, form.DeleteEpisodes));
								}
							}
						}
					}
				}
			}
		}

		void importButton_Click(object sender, EventArgs e)
		{
			using (OpenFileDialog dialog = new OpenFileDialog())
			{
				dialog.Title = "Import from file";
				dialog.DefaultExt = OpmlDocument.FileExtension;
				dialog.Filter = OpmlDocument.FileFilter;
				dialog.Multiselect = false;

				if (DialogResult.OK == dialog.ShowDialog())
				{
					this.ImportFile(dialog.FileName);
				}
			}
		}

		private void ImportFile(string path)
		{
			EventHandler<FilePersistenceEventArgs> importEvent = ImportPodcasts;

			if (importEvent != null)
			{
				importEvent(this, new FilePersistenceEventArgs(path));
			}
		}

		void exportButton_Click(object sender, EventArgs e)
		{
			using (SaveFileDialog dialog = new SaveFileDialog())
			{
				dialog.Title = "Export to file";
				dialog.DefaultExt = OpmlDocument.FileExtension;
				dialog.Filter = OpmlDocument.FileFilter;

				if (DialogResult.OK == dialog.ShowDialog())
				{
					EventHandler<FilePersistenceEventArgs> exportEvent = ExportPodcasts;

					if (exportEvent != null)
					{
						exportEvent(this, new FilePersistenceEventArgs(dialog.FileName));
					}
				}
			}
		}


	}
}
